package com.redoor.app.stake.basicdata.controller;

import com.redoor.app.stake.auth.service.BaseUserService;
import com.redoor.app.stake.basicdata.commons.BasicDataStatus;
import com.redoor.app.stake.basicdata.model.BaseStake;
import com.redoor.app.stake.basicdata.model.BussinessTypeEnum;
import com.redoor.app.stake.basicdata.model.controllerModel.BaseStakeCriteria;
import com.redoor.app.stake.basicdata.model.controllerModel.BaseStakeModifyBean;
import com.redoor.app.stake.basicdata.model.serviceModel.BaseStakeServiceBean;
import com.redoor.app.stake.basicdata.service.BaseChargerService;
import com.redoor.app.stake.basicdata.service.BaseStakeService;
import com.redoor.app.stake.common.controller.BaseController;
import com.redoor.app.stake.common.model.Page;
import com.redoor.app.stake.common.utils.JsonUtils;
import com.redoornetwork.framework.core.response.ResponseResult;
import com.redoornetwork.framework.core.response.ResultGenerator;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
* Created by wangjunlei on 2018-02-12 10:08:10.
*/
@RestController
@RequestMapping("/base/stakes")
public class BaseStakeController extends BaseController {

    @Autowired
    private BaseStakeService baseStakeInfoService;


    @Autowired
    private BaseUserService baseUserService;

    /**
     * 添加
     * @param baseStakeInfo
     * @return
     */
    @RequiresPermissions("base:stake:insert")
    @PostMapping(value = "/add")
    public ResponseResult add(@RequestBody BaseStake baseStakeInfo) {
        baseStakeInfoService.saveStake(baseStakeInfo, BussinessTypeEnum.TRACE_TYPE.WEB,null);
        return ResultGenerator.generatorSuccess();
    }

    @RequiresPermissions("base:stake:delete")
    @PostMapping("/delete")
    public ResponseResult delete(@RequestParam(value = "id",defaultValue = "") String id) {
        baseStakeInfoService.deleteStake(id, BussinessTypeEnum.TRACE_TYPE.WEB,null);
        return ResultGenerator.generatorSuccess();
    }

    @RequiresPermissions("base:stake:update")
    @PostMapping(value = "/update")
    public ResponseResult update(@RequestBody BaseStakeModifyBean modifyBean) {
        int result = baseStakeInfoService.updateStakeModify(modifyBean, BussinessTypeEnum.TRACE_TYPE.WEB, null);
        if (0 == result){
            return ResultGenerator.generatorFail(BasicDataStatus.OTHER_EXCEPTION);
        }else {
            return ResultGenerator.generatorSuccess();
        }
    }

    @PostMapping("/detail")
    public ResponseResult detail(@RequestParam(value = "id",defaultValue = "") String id) {
        BaseStakeServiceBean baseStake = baseStakeInfoService.stakeDetail(id);
        if (baseStake!=null){
            /*****begin 功能描述:去掉了BaseStakeServiceBean中的UseAt(注册码使用时间)  author:zhangMumu  2018/4/2 0002 20:32*****/
            BaseStakeServiceBean serviceBean = JsonUtils.readValue(JsonUtils.toJson(baseStake), BaseStakeServiceBean.class);
            /*****end 功能描述:去掉了BaseStakeServiceBean中的UseAt(注册码使用时间)  author:zhangMumu  2018/4/2 0002 20:32*****/

//            List<BaseChargerServiceBean> chargers = baseChargerService.queryChargersByStakeUuid(serviceBean.getUuid());
//            serviceBean.setChargers(chargers);// 充电枪

            return ResultGenerator.generatorSuccess(serviceBean);
        }else{
            return ResultGenerator.generatorFail(BasicDataStatus.STAKE_IS_EXISTS);
        }
    }

    @RequiresPermissions("base:stake:list")
    @PostMapping(value = "/list")
    public ResponseResult list(BaseStakeCriteria criteria) {
        List<String> userCode = this.baseUserService.districtPermission();
        criteria.setUncode(userCode);
        int count = baseStakeInfoService.queryStakeCount(criteria);
        if(count > 10000) {
            if(criteria.getLimit() < 0 ){
                criteria.setLimit(100);
            }
        }
        List<BaseStakeServiceBean> list = baseStakeInfoService.listServiceStake(criteria);

        Page page = new Page();
        page.setData(list);
        page.setTotal(count);

        return ResultGenerator.generatorSuccess(page);
    }


    /**
     * 根据站获取桩列表（数据量小，前端分页）
     * @param stationuuid
     * @return
     */
    @PostMapping(value = "/listByStation")
    public ResponseResult listByStation(String stationuuid) {
        BaseStakeCriteria criteria = new BaseStakeCriteria();
        criteria.setStationuuid(stationuuid);
        List<BaseStake> list = baseStakeInfoService.queryStake(criteria);

        Page page = new Page();
        page.setData(list);
        page.setTotal(list.size());

        return ResultGenerator.generatorSuccess(page);
    }
}
